package com.jxc.service.impl;
 
import java.math.BigInteger; 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.Tonline;
import com.jxc.model.po.User;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.Online;
import com.jxc.service.OnlineServiceI;


@Service("onlineService")
public class OnlineServiceImpl implements OnlineServiceI {

	private BaseDaoI<Tonline> onlineDao;

	public BaseDaoI<Tonline> getOnlineDao() {
		return onlineDao;
	}

	@Autowired
	public void setOnlineDao(BaseDaoI<Tonline> onlineDao) {
		this.onlineDao = onlineDao;
	}

	/* (non-Javadoc)
	 * @see com.jxc.service.OnlineServiceI#saveOrUpdateTonlineByLoginNameAndIp(java.lang.String, java.lang.String, java.lang.Long)
	 */
	public void saveOrUpdateTonlineByLoginNameAndIp(String loginName, String ip,Long cid) {
		Tonline t = onlineDao.get("from Tonline t where t.cname = ? and t.cip = ?", new Object[] { loginName, ip });
		if (t != null) {
			 t.setCdatetime(new Date());
		} else {
			try {
				Tonline o = new Tonline();
				o.setCdatetime(new Date());
				o.setCname(loginName);
				o.setCip(ip);
				o.setUser(new User(cid));
				onlineDao.save(o);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void deleteTonlineByLoginNameAndIp(String loginName, String ip) {
		Tonline t = onlineDao.get("from Tonline t where t.cname = ? and t.cip = ?", new Object[] { loginName, ip });
		if (t != null) {
			onlineDao.delete(t);
		}
	}

	public DataGrid datagrid(Online online) {
		DataGrid j = new DataGrid();
		j.setRows(find(online));
		j.setTotal(total(online));
		return j;
	}

	private List<Online> find(Online online) {
		List<Online> list = new ArrayList<Online>();
		try {
			String hql = "";
			if(online.getUsertype()==2){
				//代理商
				hql = " select t.cname,t.cid from qxgl_tonline t where t.cid in (select id from qxgl_user where " +
				" user_id="+online.getUid()+" and isdeleted=0) or t.cid in (select dealer_id from qxgl_dealer_grant_record " +
				" where is_granted=1 and agency_id="+online.getUid()+") group by t.cid ";
			}else{
				if(online.getUsertype()==0){
					hql = " select t.cname,t.cid from qxgl_tonline t group by t.cid" ;
				}else{
				//经销商店长营业员
				hql = " select t.cname,t.cid from qxgl_tonline t where t.cid in (select id from qxgl_user where " +
					" user_id="+online.getUid()+" and isdeleted=0) or t.cid in (select agency_id from qxgl_dealer_grant_record " +
					" where is_granted=1 and dealer_id="+online.getUid()+") group by t.cid ";
				}
			}
			List<Object> parms = new ArrayList<Object>(); 
			if (online.getSort() != null && online.getOrder() != null) {
				hql += " order by " + online.getSort() + " " + online.getOrder();
			}
			
			List<Object[]> ol = onlineDao.findSql(hql, parms, online.getPage(), online.getRows());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			for(Object[] ob:ol){
				 for (int i = 0; i < ob.length; i++) {
					Online line  = new Online();
					line.setCname(ob[i].toString());
					line.setCid(Long.parseLong(ob[++i].toString()));
					list.add(line);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	private Long total(Online online) {
		String hql = "";
		if(online.getUsertype()==2){
			//代理商
			hql = " select count(*) from (select count(*)  from qxgl_tonline t where t.cid in (select id from qxgl_user where " +
			" user_id="+online.getUid()+" and isdeleted=0) or t.cid in (select dealer_id from qxgl_dealer_grant_record " +
			" where is_granted=1 and agency_id="+online.getUid()+") group by t.cid)aa ";
		}else{
			if(online.getUsertype()==0){
				//管理员
				hql = "select count(*) from (select  count(*) from qxgl_tonline t group by t.cid)aa" ;
			}else{
				//经销商店长营业员
				hql = "select count(*) from (select count(*) from qxgl_tonline t where t.cid in (select id from qxgl_user where " +
					" user_id="+online.getUid()+" and isdeleted=0) or t.cid in (select agency_id from qxgl_dealer_grant_record " +
					" where is_granted=1 and dealer_id="+online.getUid()+") group by t.cid)aa ";
			}
		} 
		BigInteger b = onlineDao.countSQL(hql);
		return  b.longValue();
	}
 



}
